package cn.pxs.controller;

import cn.pxs.entity.Hot;
import cn.pxs.entity.LargeFile;
import cn.pxs.service.HotService;
import cn.pxs.service.LargeFileService;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.List;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping("/api/adm/hot")
public class HotController {
    @Autowired
    private HotService hotService;
    @Autowired
    private LargeFileService largeFileService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Map findAll(){
        int n = 200;
        List<Hot> hots = hotService.findAll();
        if(hots == null){
            n = 400;
            return RespJson.pack(n,"失败了",hots);
        }
        return RespJson.pack(n,"成功了",hots);
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    //地址栏方式 绑定 id
    public Map delById(@PathVariable("id")Integer id){
        int n = 200;
        if(id == null){
            n = 400;
            return RespJson.pack(n,"没id哦..",null);
        }
        List<Hot> hots = hotService.del(id);
        return RespJson.pack(n,"删除成功",hots);
    }

    @PostMapping("/addHot")
    @ResponseBody
    //通过 绑定pic 然后 获取 文件/图片
    public Map addHot(@RequestParam(value = "pic",required = false)MultipartFile file
            ,Hot hot,LargeFile largeFile){
        if(file == null) return RespJson.pack(500,"莫得图片",null);
        Hot addHot = hotService.addHot(largeFile, file, hot);
        return RespJson.pack(200,"成了",addHot);
    }

}
